package com.ruoyi.property.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.property.domain.MeterReading;
import com.ruoyi.property.service.IMeterReadingService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 抄表信息Controller
 * 
 * @author sux
 * @date 2021-07-01
 */
@Controller
@RequestMapping("/property/meterReading")
public class MeterReadingController extends BaseController
{
    private String prefix = "property/meterReading";

    @Autowired
    private IMeterReadingService meterReadingService;

    @RequiresPermissions("property:meterReading:view")
    @GetMapping()
    public String meterReading()
    {
        return prefix + "/meterReading";
    }

    /**
     * 查询抄表信息列表
     */
    @RequiresPermissions("property:meterReading:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MeterReading meterReading)
    {
        startPage();
        List<MeterReading> list = meterReadingService.selectMeterReadingList(meterReading);
        return getDataTable(list);
    }

    /**
     * 导出抄表信息列表
     */
    @RequiresPermissions("property:meterReading:export")
    @Log(title = "抄表信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MeterReading meterReading)
    {
        List<MeterReading> list = meterReadingService.selectMeterReadingList(meterReading);
        ExcelUtil<MeterReading> util = new ExcelUtil(MeterReading.class);
        return util.exportExcel(list, "meterReading");
    }

    /**
     * 新增抄表信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存抄表信息
     */
    @RequiresPermissions("property:meterReading:add")
    @Log(title = "抄表信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MeterReading meterReading)
    {
        return toAjax(meterReadingService.insertMeterReading(meterReading));
    }

    /**
     * 修改抄表信息
     */
    @GetMapping("/edit/{meterId}")
    public String edit(@PathVariable("meterId") Long meterId, ModelMap mmap)
    {
        MeterReading meterReading = meterReadingService.selectMeterReadingById(meterId);
        mmap.put("meterReading", meterReading);
        return prefix + "/edit";
    }

    /**
     * 修改保存抄表信息
     */
    @RequiresPermissions("property:meterReading:edit")
    @Log(title = "抄表信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MeterReading meterReading)
    {
        return toAjax(meterReadingService.updateMeterReading(meterReading));
    }

    /**
     * 删除抄表信息
     */
    @RequiresPermissions("property:meterReading:remove")
    @Log(title = "抄表信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(meterReadingService.deleteMeterReadingByIds(ids));
    }

    /**
     * 导入方法
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) {
        ExcelUtil<MeterReading> util = new ExcelUtil(MeterReading.class);
        try {
            List<MeterReading> meterReadingList = util.importExcel(file.getInputStream());
            String message = meterReadingService.importMeterReading(meterReadingList, updateSupport);
            return AjaxResult.success(message);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
}
